<template>
<!-- 主框架 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="outFn">退出</el-button>
    </el-header>


    <!-- 身体部 -->
    <el-container>
      <!-- 左侧菜单 -->
      <el-aside :width="isColl ? '64px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <!-- 菜单组件 -->
        <div class="toggle-button" @click="spanFn">
          导航栏
        </div>
        <el-menu 
        :collapse="isColl" 
        background-color="#333744" 
        text-color="#fff"
        active-text-color="red" 
        unique-opened
        :collapse-transition='false'
        router
        default-active=''
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuArr" :key="item.id">
            <!-- 一级菜单内容区 -->
            <template slot="title">
              <i class="el-icon-eleme"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + obj.path + ''" v-for="obj in item.children" :key="obj.id">
              <!-- 二级菜单内容区 -->
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{obj.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>


      <!-- 右侧内容 -->
      <el-main>
        <!-- 子路由组件 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
//导入请求菜单API
import { getMenusAPI } from '@/api'
export default {
  data(){
    return {
      //菜单数组数据
      menuArr:[],
      isColl:false
    }
  },
  methods: {
    outFn() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    spanFn(){
      this.isColl = !this.isColl
    }
  },
  async created() {
    //调用接口获取菜单数组数据
    const { data } = await getMenusAPI()
    this.menuArr = data.data
    console.log(this.menuArr);
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  height: 40px;
}
</style>
